package com.stqf.scala.grammar.collection

import java.util

import scala.collection.mutable.ArrayBuffer

/**
  * Scala之Array学习
  * Created by ASUS on 2018-02-01.
  */
object ArrayLearn {
  def main(args: Array[String]): Unit = {
    val arr1 = new Array[Int](5)
    arr1(0) = 23
    arr1(1) = 46
    arr1(2) = 18
    arr1(3) = 20
    arr1(4) = 61
    val arr2 = new Array[Int](5)
    arr2(0) = 23
    arr2(1) = 18
    arr2(2) = 22
    arr2(3) = 51
    arr2(4) = 61
    println("数组合并:" + util.Arrays.toString(Array.concat(arr1, arr2)))
    var arr3 = Array.range(2, 9)
    println("创建区间数组:" + util.Arrays.toString(arr3))
    var arr4 = new Array[Int](10)
    Array.copy(arr3, 1, arr4, 2, 6)
    println("数组复制:" + util.Arrays.toString(arr4))
    scala.util.Sorting.quickSort(arr2)
    println("数组排序:" + util.Arrays.toString(arr2))
    println("数组打印:" + arr3.mkString("\t"))
    //定长数组的定义
    val basicType: Array[Int] = new Array[Int](10)
    val complexType: Array[String] = new Array[String](5)
    println("基本类型数组:" + util.Arrays.toString(basicType))
    println("复杂类型数组:" + complexType.toList)
    //变长数组的定义
    val variableArray = new ArrayBuffer[String]()
    //尾部添加一个或多个元素
    variableArray += ("aync")
    variableArray += ("Hello", "world")
    //尾部添加数组或集合
    variableArray ++= Array("synchronized", "description")
    println("变长数组声明:")
    variableArray.foreach(temp => print(temp + ","))
    //从尾部删除指定个元素
    variableArray.trimEnd(2)
    println("\n删除末尾2个元素:")
    variableArray.foreach(temp => print(temp + ","))
    //指定位置插入元素
    variableArray.insert(0, "first")
    println("\n索引为0出插入一个元素:")
    variableArray.foreach(temp => print(temp + ","))
    //转成定长数组
    val arr = variableArray.toArray
    println("\n转成定长数组:" + arr.toList)

  }
}
